package com.test.drawing;

import javax.microedition.khronos.opengles.GL10;

/**
 * Animation Class.<br>
 * Same as a Mesh, but it has changeable vertices when drawing.
 */
public class Animation extends Mesh {
	
    // Minimum Animation Transition Time (ms)
    private static final long MINIMUM_ANIMATION_TIME = 25;
    private long animation_time = MINIMUM_ANIMATION_TIME;
    private long currentTime;
    private long lastTime = 0;
    private long timeAfter;

    private boolean animationIsActive = false;
    private boolean stopAnimation = false;

    private int currentFrame = 0;
    
    private AnimationBuffer anBuffer;

    /**
     * Class Constructor.
     * 
     * @param scene
     *            Scene which will define the Animation
     */
    public Animation(AnimationBuffer anBuffer) {
    	super(anBuffer);
    	this.anBuffer = anBuffer;
    }

    /**
     * Activates the Animation only if it is not already active.
     */
    public void playAnimation() {
        if (!animationIsActive && !stopAnimation)
            animationIsActive = true;
    }

    /**
     * Stops the active Animation.
     */
    public void stopAnimation() {
        if (animationIsActive) stopAnimation = true;
    }

    /**
     * Verify if the Animation is being Played.
     * 
     * @return True - Animation is being played<br>
     *         False - Animation is stopped
     */
    public boolean isAnimationActive() {
        return animationIsActive;
    }

    /**
     * Render the animation.
     * 
     * @param gl
     *            the OpenGL context to render to.
     */

    @Override
    public void draw(GL10 gl) {

        if (animationIsActive && anBuffer.getFramesBufferSize() > 1) {

            // Here is Defined the Vertices Buffer to be used
            // As Animation is may change...

            currentTime = System.currentTimeMillis();

            // Last Frame
            if (currentFrame == anBuffer.getFramesBufferSize() - 1) {
                if (stopAnimation) {
                    animationIsActive = false;
                    stopAnimation = false;
                }
            }

            if (currentTime - lastTime >= animation_time) { // Changes a Frame

                currentFrame = (currentFrame + 1) % anBuffer.getFramesBufferSize();
                
                anBuffer.setNextFrame(currentFrame);
//                anBuffer.setCurrentVerticeBuffer(currentFrame);
                lastTime = currentTime;

            }
        }
        super.draw(gl);

        // Defines the Animation time on run-time
        // to gives Smooth animation

        if (animationIsActive) {
            timeAfter = System.currentTimeMillis();
            animation_time = currentTime - timeAfter;
            if (animation_time < MINIMUM_ANIMATION_TIME) {
                animation_time = animation_time + MINIMUM_ANIMATION_TIME;
            }
        }
    }
}
